package com.leyou.auth.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.security.PrivateKey;
import java.security.PublicKey;

@Data
@Slf4j
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties {

    //公钥地址
    private String pubKeyPath;
    //私钥地址
    private String priKeyPath;
    //用户token相关属性
    private UserTokenProperties user = new UserTokenProperties();

    //需要直接在这个类里面获取公钥私钥
    private PublicKey publicKey;
    private PrivateKey privateKey;

    //内部类方式创建UserTokenProperties
    @Data
    public class UserTokenProperties {
        private Integer expire;  //token过期时长
        private Integer minRefreshInterval;
        private String cookieName;  //存放token的cookie名称
        private String cookieDomain;  //存放token的cookie的domain
    }



    /**
     * <bean id="" class="" init-method="" destroy-metod=""></bean>
     */
    @PostConstruct   //由于需要先注入地址后,使用地址生成公钥私钥,所以要这个注解,这个注解是在类初始化后调用

    public void init() throws Exception {
        publicKey = RsaUtils.getPublicKey(pubKeyPath);
        privateKey = RsaUtils.getPrivateKey(priKeyPath);
    }

}